package Greedy_Snake_Map;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.Timer;


import Main.mainPanel;
import Mouse_key_Event.Key;
import snake.Snake_body;

public class Time {
	private static Timer time;
	private Snake_body snake;
	private JScrollBar scrolbarx,scrolbary;
	private JScrollPane scrollpane;
	private mainPanel panel;
	private static long  manyTime=0;
	public Time(JScrollPane scrollpane,Snake_body snake,mainPanel panel)
	{
		this.scrollpane=scrollpane;
		this.snake=snake;
		this.panel=panel;
		set_timer_to_scrollpanemove();
	}
	public void set_timer_to_scrollpanemove()
	{
		if(scrollpane==null) return ;
		time=new Timer(1,new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				
				manyTime+=1;
				scrolbarx=scrollpane.getHorizontalScrollBar();
				scrolbary=scrollpane.getVerticalScrollBar();
				
				double Screenx=(scrollpane.getViewport().getComponent(0).getWidth()*(1-(2/mainPanel.getScalex())))/MMap.get_x();
				double Screeny=(scrollpane.getViewport().getComponent(0).getHeight()*(1-(2/mainPanel.getScaley())))/MMap.get_y();
				//double afterDistancex=(scrolbarx.getMaximum()/2-scrolbarx.getWidth()/2+(snake.body.get(0).x-map.get_x()/2)*Screenx)-scrolbarx.getValue();
				//double afterDistancey=scrolbary.getMaximum()/2-scrolbary.getHeight()/2+(snake.body.get(0).y-map.get_y()/2)*Screeny-scrolbary.getValue();
				scrolbarx.setValue((int) (scrolbarx.getMaximum()/2-scrolbarx.getWidth()/2+(snake.body.get(0).x-MMap.get_x()/2)*Screenx));
				scrolbary.setValue((int) (scrolbary.getMaximum()/2-scrolbary.getHeight()/2+(snake.body.get(0).y-MMap.get_y()/2)*Screeny));
				
				if(!Key.Stop)
				{
					scrolbarx.setValue(scrolbarx.getValue());
				}
			}});
		time.start();
		
	}
	public Timer getTime()
	{
		return time;
	}
	static public long getManyTime()
	{
		return manyTime;
	}
	static public void setManyTime(long time)
	{
		manyTime=time;
	}
}
